/*
 * Copyright 2012 Sedov Eduard.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.es.registry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Sedov Eduard
 */
public class RegistryListenerSupport implements RegistryListener {

    protected List<RegistryListener> listeners;
    protected Registry overrideRegistry;

    public RegistryListenerSupport() {
    }

    public RegistryListenerSupport(Registry overrideRegistry) {
        this.overrideRegistry = overrideRegistry;
    }

    public Registry getOverrideRegistry() {
        return overrideRegistry;
    }

    public void setOverrideRegistry(Registry overrideRegistry) {
        this.overrideRegistry = overrideRegistry;
    }

    /**
     * Puts the given listener to internal listener list.
     *
     * @param listener the listner to put
     */
    public void addListener(RegistryListener listener) {
        if (listeners == null) {
            listeners = new ArrayList();
        }
        if (listener != null && !listeners.contains(listener)) {
            listeners.add(listener);
        }
    }

    /**
     * Removes the given listener from internal listener list.
     *
     * @param listener the listener to remove
     */
    public void removeListener(RegistryListener listener) {
        if (listeners != null) {
            listeners.remove(listener);
        }
    }

    /**
     * Returns all registered listeners.
     *
     * @return a list with all registered listeners
     */
    public List<RegistryListener> getListeners() {
        if (listeners == null) {
            return Collections.EMPTY_LIST;
        }
        return new ArrayList(listeners);
    }

    protected void fireOnCd(Registry registry, String currentName) {
        if (listeners != null) {
            for (RegistryListener registryListener : listeners) {
                registryListener.onCd(overrideRegistry != null ? overrideRegistry : registry, currentName);
            }
        }
    }

    protected void fireOnCd(Registry registry, String oldName, String newName) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onCd(registry, oldName, newName);
            }
        }
    }

    protected void fireOnMkdir(Registry registry, String name) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onMkdir(registry, name);
            }
        }
    }

    protected void fireOnRmdir(Registry registry, String name) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onRmdir(registry, name);
            }
        }
    }

    protected void fireOnGet(Registry registry, String name, Object value) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onGet(registry, name, value);
            }
        }
    }

    protected void fireOnPut(Registry registry, String name, Object oldValue, Object newValue) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onPut(registry, name, oldValue, newValue);
            }
        }
    }

    protected void fireOnDelete(Registry registry, String name, Object value) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onDelete(registry, name, value);
            }
        }
    }

    protected void fireOnList(Registry registry, Set<String> result) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onList(registry, result);
            }
        }
    }

    protected void fireOnRename(Registry registry, String oldName, String newName) {
        if (listeners != null) {
            registry = overrideRegistry != null ? overrideRegistry : registry;
            for (RegistryListener registryListener : listeners) {
                registryListener.onRename(registry, oldName, newName);
            }
        }
    }

    public void onCd(Registry registry, String currentName) {
        fireOnCd(registry, currentName);
    }

    public void onCd(Registry registry, String oldName, String newName) {
        fireOnCd(registry, oldName, newName);
    }

    public void onMkdir(Registry registry, String name) {
        fireOnMkdir(registry, name);
    }

    public void onRmdir(Registry registry, String name) {
        fireOnRmdir(registry, name);
    }

    public <T> void onGet(Registry registry, String name, T value) {
        fireOnGet(registry, name, value);
    }

    public <T> void onPut(Registry registry, String name, T oldValue, T newValue) {
        fireOnPut(registry, name, oldValue, newValue);
    }

    public <T> void onDelete(Registry registry, String name, T value) {
        fireOnDelete(registry, name, value);
    }

    public void onList(Registry registry, Set<String> result) {
        fireOnList(registry, result);
    }

    public void onRename(Registry registry, String oldName, String newName) {
        fireOnRename(registry, oldName, newName);
    }
}
